package io.renren.modules.basic.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.basic.entity.MtCcDeviceCommunLogEntity;
import io.renren.modules.basic.service.MtCcDeviceCommunLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-10-14 10:50:42
 */
@RestController
@Api(description = "硬件通讯信息")
@RequestMapping("generator/mtccdevicecommunlog")
public class MtCcDeviceCommunLogController {
    @Autowired
    private MtCcDeviceCommunLogService mtCcDeviceCommunLogService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:mtccdevicecommunlog:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mtCcDeviceCommunLogService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:mtccdevicecommunlog:info")
    public R info(@PathVariable("id") Integer id){
		MtCcDeviceCommunLogEntity mtCcDeviceCommunLog = mtCcDeviceCommunLogService.getById(id);

        return R.ok().put("mtCcDeviceCommunLog", mtCcDeviceCommunLog);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:mtccdevicecommunlog:save")
    public R save(@RequestBody MtCcDeviceCommunLogEntity mtCcDeviceCommunLog){
        mtCcDeviceCommunLog.setCreateTime(new Date());
		mtCcDeviceCommunLogService.save(mtCcDeviceCommunLog);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:mtccdevicecommunlog:update")
    public R update(@RequestBody MtCcDeviceCommunLogEntity mtCcDeviceCommunLog){
        mtCcDeviceCommunLog.setUpdateTime(new Date());
		mtCcDeviceCommunLogService.updateById(mtCcDeviceCommunLog);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:mtccdevicecommunlog:delete")
    public R delete(@RequestBody Integer[] ids){
		mtCcDeviceCommunLogService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/list/for/excel")
    @ApiOperation(value = "导出excel" , notes = "导出excel")
    public void listForExcel(HttpServletResponse response, @RequestParam Map<String, Object> params)throws IOException {
        List<MtCcDeviceCommunLogEntity> mtCcDeviceCommunLogEntityList =
                mtCcDeviceCommunLogService.list();
        //指定列表标题和工作表名称
        ExportParams param = new ExportParams("硬件通讯信息报表","sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(param,MtCcDeviceCommunLogEntity.class,mtCcDeviceCommunLogEntityList);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type","application/vnd.ms-excel");
//        response.setHeader("Content-Disposition","attachment;filename="+System.currentTimeMillis()+".xls");
        String fileName = "硬件通讯信息报表统计";
        response.setHeader("Content-Disposition","attachment;filename="+ new String(fileName.getBytes("utf-8"),"ISO-8859-1" ) +".xls");

        try {
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
